package cn.ycq.im.chat.netty;

import cn.ycq.im.chat.MyWebSocketHandler;
import cn.ycq.im.http.DefaultHttpRequestHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.timeout.IdleStateHandler;

public class MyChannelInitializer extends ChannelInitializer<SocketChannel>{

    private String wsUrl;
    public MyChannelInitializer(String wsUrl) {
        this.wsUrl = wsUrl;
    }

    @Override
    protected void initChannel(SocketChannel sc) throws Exception {
        ChannelPipeline pipeline = sc.pipeline();

        pipeline.addLast(new HttpServerCodec());
        pipeline.addLast("aggregator",new HttpObjectAggregator(1024 * 8));
        pipeline.addLast(new WebSocketServerProtocolHandler(wsUrl));


        pipeline.addLast(new DefaultHttpRequestHandler());
        pipeline.addLast(new IdleStateHandler(10,30,60));
        pipeline.addLast(new MyWebSocketHandler());


    }
}
